/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.tbp.utils;

import java.net.URL;
import javax.swing.ImageIcon;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.JComponent;

/**
 *
 * @author Diego
 */
public class ImageUtils {

    private static final int DEFAULT_WIDTH = 171;
    private static final int DEFAULT_HEIGHT = 157;
    public final static String jpeg = "jpeg";
    public final static String jpg = "jpg";
    public final static String gif = "gif";
    public final static String tiff = "tiff";
    public final static String tif = "tif";
    public final static String png = "png";
    public final static String bmp = "bmp";

    public static ImageIcon createIcon(String arg) {
        URL url = ClassLoader.getSystemResource("br/com/tbp/resources/" + arg);
        if (url != null) {
            return new ImageIcon(url);
        }
        return null;
    }

    public static URL createIconURL(String arg) {
        URL url = ClassLoader.getSystemResource("br/com/tbp/resources/" + arg);
        return url;
    }

    /*
     * Get the extension of a file.
     */
    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 && i < s.length() - 1) {
            ext = s.substring(i + 1).toLowerCase();
        }
        return ext;
    }

    public static Image scaleImage(Image img, JComponent container) {

        int w = container.getWidth();
        if (w <= 0) {
            //w = d.width;
            w = DEFAULT_WIDTH;
        }
        int h = container.getHeight();
        if (h <= 0) {
            h = DEFAULT_HEIGHT;
        }

        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(img, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }
}
